<?php


namespace Frieza\Modules\Position\Actions;

use Doraemon\model\MemberExtendInfo;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class NickWhite extends RowAction
{
    public $name = '昵称白名单';

    public function handle($uid,Request $request)
    {
        $nickWhite = $request->get('white_nick');
        if ($nickWhite) {
            $nickWhite = str_replace(array("\r\n", "\r", "\n"), ",", $nickWhite);
        }
        $res = MemberExtendInfo::addFieldByUid($uid,['nick_white' => $nickWhite]);
        if ($res){
            return $this->response()->success('昵称白名单添加成功!')->refresh();
        }else{
            return $this->response()->error('昵称白名单添加失败,稍后重试!');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form($request)
    {
        $uid = $request->uid;
        $info = MemberExtendInfo::getInfo($uid);
        $nick_white = '';
        if (!empty($info) && !empty($info['nick_white'])) {
            $nick_white = $info['nick_white'];
            $nick_white = str_replace(',', PHP_EOL, $nick_white);
        }
        $this->textarea('white_nick', '昵称白名单')->required()->default($nick_white)->help('每行一个白名单词');

    }

}